// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { companyBase } from "../../../../script/game/control/Item/ItemBase";
import { playerType } from "../../../../script/game/gameType";
import figureModel from "../../../../script/game/model/figureModel";
import { ListViewAdapter } from "../../../../script/util/ListView/ListViewAdapter";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(ListViewAdapter)
    list: ListViewAdapter = null;
    
    companyData: companyBase = null;

    start () {
        this.registerEvent("hireCeo", (data: playerType) => {
            let su = this.companyData.hireCeo(data);
            if(su) {
                ccs.mainEmitter.emit('upInfo');
                this.nodes.cur.active = true;
                this.list.node.active = false;
                this.setCeoInfo();
            }
        })
        this.companyData = game.playerMgr.bag.companyBag.getItem(this._args.id);
        if(this.companyData.company.ceo) {
            this.nodes.cur.active = true;
            this.list.node.active = false;
            this.nodes.btn.active = false;
            this.setCeoInfo();
            this.touchEndEventScale(this.nodes.btn_unHire, () => {
                this.companyData.unHireCeo();
                //更新视图
                this.showList();
                this.nodes.cur.active = false;
                ccs.mainEmitter.emit('upInfo');
            })
        } else {
            this.showList();
            this.nodes.cur.active = false;
        }
    }
    setCeoInfo() {
        let ceo = this.companyData.company.ceo;
        this.nodes.ceoName.getComponent(cc.Label).string = ceo.name;
        this.nodes.ceoAge.getComponent(cc.Label).string = "年龄：" + Math.trunc(ceo.age);
        this.nodes.ceoLevel.getComponent(cc.Label).string ="专业等级：" + ceo.work.level.toString();
        this.nodes.ceoLevelName.getComponent(cc.Label).string ="职称：" + ceo.work.levelName[ceo.work.level];
        this.nodes.wage.getComponent(cc.Label).string = "工资：" + ccs.gf.getOmitNum(ceo.work.income);
        this.nodes.talent.getComponent(cc.Label).string = `智商：${ccs.gf.getOmitNum(ceo.iq)}，情商：${ccs.gf.getOmitNum(ceo.eq)}，魅力：${ccs.gf.getOmitNum(ceo.charm)}`;
    }
    showList() {
        if(this.companyData.company.resumeList && this.companyData.company.resumeList.length > 0) {
            this.nodes.btn.active = false; //招聘方式
            this.list.node.active = true;
            this.list.updateItems(this.companyData.company.resumeList);
        } else {
            this.nodes.btn.active = true;
            this.list.node.active = false;
            this.btnInit();
        }
    }
    btnInit() {
        this.nodes.btn.children.forEach((val, index) => {
            this.touchEndEventScale(val, () => {
                this.randomHero(index + 1);
            })
        })
    }
    randomHero(level: number) {
        //随机人物属性
        if(level == 1) {
            //人才市场
            //魅力 ，iq/200 +work.level/10)/2;
            //charm/100 + eq/200)/2; 
            this.addHero(1);
        } else if(level == 2) {
            //校园招聘
            this.addHero(2);
        } else if(level == 3) {
            //猎头
            this.addHero(3);
        } else {
            //大公司挖人
            this.addHero(4);
        }

    }
    addHero(level) {
        if(!this.companyData.costMoney(level * 20000 * this.companyData.company.level)) {
            return ccs.winMgr.showWin('toast', { msg: "金钱不足"});
        }
        let i = 1;
        while(i < 6) {
            let hero = new figureModel("ceo");
            hero.randomCeo(level, this._args.id);
            this.companyData.addResume(hero.heroInfo);
            i++;
        }
        this.nodes.btn.active = false;
        this.list.node.active = true;
        this.list.updateItems(this.companyData.company.resumeList);
    }
}
